<?php
/*
 * Created on Jan 20, 2013
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
function _frontend_preset_custom_products_icons($view){
	static $start;
    static $rows;
    
    /*
    if (!empty($start)) {
    	$start = 0;
    }else{
    	$start += $rows;
    }
    */
    
    switch ($view['tag']) {
    	case 'MOSTFAVORITE':
        return _frontend_preset_custom_products_icons_favorite($start, $rows, true);
    }

}

function _frontend_preset_custom_products_icons_favorite($start, $rows, $pager){
	
    if (true===$pager) {
    	$query =db_select('node', 'n')->extend('GloolsPagerDefault');
        $query->addMetaData('page-owner', 'products/icons/favorite');
    }else{
    	$query =db_select('node', 'n');
    }
    
    $query->fields('n', array('nid', 'sticky', 'created'))
          ->condition('n.type', 'product')
           ->condition('n.status', 1)
           ->condition('n.promote', 1)
           ->orderBy('n.sticky', 'DESC')
           ->orderBy('n.created', 'DESC')
           ->addTag('node_access');
    
    if (true===$pager) {
        $query->limit(6);//variable_get('default_nodes_main', 15)
    }
    
    $nids = $query->execute()->fetchCol();
    
    if (!empty($nids)) {
        $nodes = node_load_multiple($nids);
        $build = node_view_multiple($nodes, 'icon');
        
        if (true==$pager) {
        	$build['pager'] = array(
              '#theme'   => 'glools_pager',
              '#weight'  => 5,
              '#element' => 'products/icons/favorite'
            );
        }
    } else {
    	$build['#children'] =t('There is no product published.');
    }
    return $build;
}